package com.github.baichuan.application.scaffold.jpa.application;

import com.github.baichuan.application.scaffold.domain.application.Application;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Builder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

@Builder
public class ApplicationSpecification implements Specification<Application> {

    private String realmId;

    @Override
    public Predicate toPredicate(Root<Application> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        Predicate predicate = builder.conjunction();

        if(StringUtils.isNotBlank(realmId)){
            predicate = builder.and(predicate, builder.equal(root.get("realm").get("id"), realmId));
        }

        return predicate;
    }
}
